package com.springboot.cloud.sysadmin.organization.service.impl;

//import com.alicp.jetcache.anno.CacheInvalidate;
//import com.alicp.jetcache.anno.CacheType;
//import com.alicp.jetcache.anno.CacheUpdate;
//import com.alicp.jetcache.anno.Cached;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.sysadmin.organization.dao.DeptMapper;
import com.springboot.cloud.sysadmin.organization.entity.param.DeptQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Dept;
import com.springboot.cloud.sysadmin.organization.service.IDeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */

@Service
@Slf4j
public class DeptService extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Override
    public boolean add(Dept dept) {

        Dept dept1 = this.getOne(new QueryWrapper<Dept>()
                .and(i -> i.eq(StrUtil.isNotBlank(dept.getParentId()), "parent_id", dept.getParentId())
                        .eq(StrUtil.isNotBlank(dept.getName()), "name", dept.getName())), false
        );
        log.debug("dept1:", dept1);
        if (ObjectUtil.isNull(dept1)) {
            if (!dept.getParentId().equals("-1")) {
                Dept pdept = getDept(dept.getParentId());
                dept.setAncestors(pdept.getAncestors() + "," + dept.getParentId());
            } else {
                dept.setAncestors("-1");
            }
            return this.save(dept);
        }
        return false;
    }

    @Override
//    @CacheInvalidate(name = "dept::", key = "#id")
    public boolean delete(Set<String> ids) {

        return this.removeByIds(ids);
    }


    @Override
//    @CacheInvalidate(name = "dept::", key = "#dept.id")
    public boolean update(Dept dept) {
        Dept dept1 = this.getOne(new QueryWrapper<Dept>()
                .and(i -> i.eq(StrUtil.isNotBlank(dept.getParentId()), "parent_id", dept.getParentId())
                        .eq(StrUtil.isNotBlank(dept.getName()), "name", dept.getName())), false
        );

        if (ObjectUtil.isNull(dept1) || dept1.getId().equals(dept.getId()) && get(dept.getId())) {
            return this.saveOrUpdate(dept);
        }
        return false;
    }

    @Override
    public boolean get(String id) {
        Dept dept = this.getById(id);
        if (ObjectUtil.isNull(dept)) {
            return false;
        }
        return true;
    }

    @Override
//    @Cached(name = "dept::", key = "#id", cacheType = CacheType.BOTH)
    public Dept getDept(String id) {
        return this.getById(id);
    }

    @Override
    public List<Dept> query(DeptQueryParam deptQueryParam) {
        SimpleDateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Dept> queryWrapper = deptQueryParam.build();
        queryWrapper.like(StrUtil.isNotBlank(deptQueryParam.getName()), "name", deptQueryParam.getName());
        queryWrapper.eq(StrUtil.isNotBlank(deptQueryParam.getStatus()), "status", deptQueryParam.getStatus());
        if (deptQueryParam.getCreatedTimeStart() != null && deptQueryParam.getCreatedTimeEnd() != null) {
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') >= date_format('" + convert.format(deptQueryParam.getCreatedTimeStart()) + "','%Y-%m-%d')");
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') <= date_format('" + convert.format(deptQueryParam.getCreatedTimeEnd()) + "','%Y-%m-%d')");
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<Dept> queryByParentId(String id) {
        return this.list(new QueryWrapper<Dept>().eq("parent_id", id));
    }

    @Override
    public List<Map<String, Object>> getAll() {
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Dept::getId, Dept::getName);
        return this.listMaps(queryWrapper);
    }
}
